<template>
	<xn-form-container
		:title="formData.id ? '编辑接口用户' : '增加接口用户'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<!--			<a-form-item label="用户名称：" name="client_id">-->
			<!--				<a-input placeholder="请输入客户ID" v-model:value="formData.client_id"></a-input>-->
			<!--			</a-form-item>-->
			<a-form-item label="环境：" name="env">
				<a-input placeholder="请输入环境" v-model:value="formData.env"></a-input>
			</a-form-item>
			<a-form-item label="客户Key：" name="client_access_key">
				<a-input placeholder="请输入客户Key" v-model:value="formData.client_access_key"></a-input>
			</a-form-item>
			<a-form-item label="access密钥：" name="access_secret">
				<a-input placeholder="请输入access密钥" v-model:value="formData.access_secret"></a-input>
			</a-form-item>
			<a-form-item label="app_id：" name="app_id">
				<a-input placeholder="请输入app_id" v-model:value="formData.app_id"></a-input>
			</a-form-item>
			<a-form-item label="项目ID：" name="project_id">
				<a-input-number
					placeholder="请输入项目ID"
					v-model:value="formData.project_id"
					style="width: 100%"
					hide-button
				></a-input-number>
			</a-form-item>
			<a-form-item label="有效时间：" name="expire_time">
				<a-date-picker v-model:value="formData.expire_time" placeholder="请选择有效时间" style="width: 100%" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required, rules } from '@/utils/formRules'
	import smCrypto from '@/utils/smCrypto'
	import dayjs from 'dayjs'
	import api from './api'
	import userApi from '../../customerInfo/api'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({
		time: []
	})

	let index = 0
	// 打开抽屉
	const onOpen = (record, id) => {
		visible.value = true
		console.log(record)
		if (record) {
			record.expire_time = dayjs(record.modified_time)
			formData.value = Object.assign({}, record)
			formData.value.time = [dayjs(record.start_time), dayjs(record.end_time)]
		} else {
			formData.value = {}
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}
	// 图标选择器回调
	const iconCallBack = (value) => {
		if (value) {
			formRef.value.clearValidate('icon')
		}
		formData.value.icon = value
	}

	const userOptions = ref([])

	// 默认要校验的
	const formRules = {
		client_id: [required('请输入用户名称')],
		env: [required('请输入环境')],
		client_access_key: [required('请输入客户Key')],
		access_secret: [required('请输入access密钥')],
		app_id: [required('请输入app_id')],
		project_id: [required('请输入项目ID')],
		expire_time: [required('请选择有效时间')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				api
					.submitForm(
						{
							...formData.value,
							...(formData.value.client_id
								? {
										create_time: dayjs(formData.value.create_time).format('YYYY-MM-DD HH:mm:ss')
								  }
								: {
										create_time: dayjs().format('YYYY-MM-DD HH:mm:ss')
								  }),
							modified_time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
							expire_time: dayjs().format('YYYY-MM-DD HH:mm:ss')
						},
						formData.value.client_id
					)
					.then(() => {
						onClose()
						emit('successful')
					})
			})
			.catch((error) => {
				console.log(error)
			})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
